﻿// -----------------------------------------------------------------------
// <copyright file="ApprovalHistoryImpl.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data.Common;
using StampController.BL.Models;
using StampController.Core;

namespace StampController.BL
{
    /// <summary>
    ///     TODO: Update summary.
    /// </summary>
    public class ApprovalHistoryImpl
    {
        public IList<ApprovalHistoryModel> GetApprovalHistoryList(int id)
        {
            var list = new List<ApprovalHistoryModel>();
            using (var dbHelper = new DbHelper())
            {
                object[,] parameter =
                    {
                        {"@RequestId", id}
                    };
                using (DbDataReader reader = dbHelper.ExecuteReader(@"
                    SELECT ApprovedBy, ApprovedDate, Comment, Status, D.Name
                    FROM ApprovalHistory A
                    LEFT JOIN Department D ON A.DeptID = D.DeptID
                    WHERE RequestId = @RequestId ORDER BY ApprovedDate", parameter))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            list.Add(new ApprovalHistoryModel
                            {
                                ApprovedBy = reader.Get<string>(0),
                                ApprovedDate = reader.Get<DateTime>(1),
                                Comment = reader.Get<string>(2),
                                Status = reader.Get<int>(3),
                                DeptName = reader.Get<string>(4)
                            });
                        }
                    }

                    reader.Close();
                }

                return list;
            }
        }

        public IList<ApprovalHistoryModel> GetApprovalHistoryList(string requestId)
        {
            var list = new List<ApprovalHistoryModel>();
            using (var dbHelper = new DbHelper())
            {
                object[,] parameter =
                    {
                        {"@RequestId", requestId}
                    };
                using (DbDataReader reader = dbHelper.ExecuteReader("SELECT ApprovedBy, ApprovedDate, Comment, Status FROM ApprovalHistory WHERE RequestId = (SELECT ID FROM RequestForm WHERE RequestId = @RequestId) ORDER BY ApprovedDate", parameter))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            list.Add(new ApprovalHistoryModel
                            {
                                ApprovedBy = reader.Get<string>(0),
                                ApprovedDate = reader.Get<DateTime>(1),
                                Comment = reader.Get<string>(2),
                                Status = reader.Get<int>(3)
                            });
                        }
                    }

                    reader.Close();
                }

                return list;
            }
        }
    }
}